package com.geek.base;

public class FinallyAndReturn {

	public static void main(String[] args) {
		FinallyAndReturn obj = new FinallyAndReturn();
		int y = obj.test();
		System.out.println(y);
		StringBuffer z = obj.test2();
		System.out.println(z);
	}


	public int test(){
		int x = 0;
		try {
			return ++x;
		}catch (Exception e){
			e.printStackTrace();
		}finally {
			//无论怎样finally都会执行，但是因为x已经return出去了，并不会变成2
			++x;
		}
		return x;
	}
	public StringBuffer test2(){
		StringBuffer x = new StringBuffer();
		try {
			x.append("try");
			return x;
		}catch (Exception e){
			e.printStackTrace();
		}finally {
			//无论怎样finally都会执行，因为x是引用类型，返回出去的是地址，因此能够获取到x的值是try&finally
			x.append("&finally");
		}
		return x;
	}

}
